package com.rxyl.love.petdoctor.acticity;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.adapter.FindDoctorAdapter;
import com.rxyl.love.petdoctor.adapter.MyListViewAdapter;
import com.rxyl.love.petdoctor.adapter.DiseaseAdapter;
import com.rxyl.love.petdoctor.constant.Urls;
import com.rxyl.love.petdoctor.entity.Disease;
import com.rxyl.love.petdoctor.entity.FindDoctor;
import com.rxyl.love.petdoctor.entity.FindHospital;
import com.rxyl.love.petdoctor.util.NetworkUtil;
import com.rxyl.love.petdoctor.util.SharedUtil;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_search)
public class SearchActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    @ViewInject(R.id.iv_search_back)
    private ImageView iv_back;

    @ViewInject(R.id.iv_search_row)
    private ImageView iv_row;

    @ViewInject(R.id.tv_search_type)
    private TextView tv_type;

    @ViewInject(R.id.tv_search_hint)
    private TextView tv_hint;

    @ViewInject(R.id.et_search)
    private EditText et_search;

    @ViewInject(R.id.iv_search_search)
    private ImageView iv_search;

    @ViewInject(R.id.rl_search_type)
    private RelativeLayout rl_type;

    @ViewInject(R.id.lv_search)
    private ListView lv_search;

    @ViewInject(R.id.gv_hotWord)
    private GridView gv_hotWord;

    private MyListViewAdapter sAdapter;
    private List<Object> mList;

    private PopupWindow popupWindow;
    private View popupView;
    private int type = 1;
    private String searchUrl;
    private ProgressDialog progressDialog;
    private String[] hotWords = {"细小", "犬瘟热", "狂犬病", "拉稀"
            , "皮肤病", "咳嗽", "绝育", "耳螨"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        iv_back.setOnClickListener(this);
        rl_type.setOnClickListener(this);
        iv_search.setOnClickListener(this);

        mList = new ArrayList<>();
        sAdapter = new MyListViewAdapter(this, mList);
        lv_search.setAdapter(sAdapter);

        ArrayAdapter gvAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, hotWords
        );
        gv_hotWord.setAdapter(gvAdapter);
        gv_hotWord.setOnItemClickListener(this);

        popupView = View.inflate(this, R.layout.popupwindow_search, null);
        TextView type1 = (TextView) popupView.findViewById(R.id.tv_pop_type1);
        TextView type2 = (TextView) popupView.findViewById(R.id.tv_pop_type2);
        TextView type3 = (TextView) popupView.findViewById(R.id.tv_pop_type3);
        type1.setOnClickListener(this);
        type2.setOnClickListener(this);
        type3.setOnClickListener(this);

        popupWindow = new PopupWindow(popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                iv_row.setImageResource(R.mipmap.search_top_up);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.iv_search_back:
                finish();
                break;
            //搜索类型
            case R.id.rl_search_type:
                selectType();
                break;
            //开始搜索
            case R.id.iv_search_search:
                startKeySearch();
                break;
            //搜索类型
            case R.id.tv_pop_type1:
                type = 1;
                tv_type.setText("找医院");
                popupWindow.dismiss();
                break;
            case R.id.tv_pop_type2:
                type = 2;
                tv_type.setText("找医生");
                popupWindow.dismiss();
                break;
            case R.id.tv_pop_type3:
                type = 3;
                tv_type.setText("查疾病");
                popupWindow.dismiss();
                break;
        }
    }


    private void selectType() {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
        popupWindow.showAsDropDown(rl_type);
        iv_row.setImageResource(R.mipmap.search_top_down);
    }

    private void startKeySearch() {
        gv_hotWord.setVisibility(View.GONE);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("玩命加载中...");
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.setCancelable(true);
        progressDialog.show();

        String keyword = et_search.getText().toString().trim();
        if (TextUtils.isEmpty(keyword)) {
            Toast.makeText(this, "请输入搜索关键字", Toast.LENGTH_SHORT).show();
            return;
        }
        tv_hint.setText(keyword + "的搜索结果为");
        tv_hint.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        //latitude=22.534686&limit=20&page=1&keyword=%E5%8C%BB%E9%99%A2&type=1&longitude=113.94471
        String latitude = SharedUtil.getString("latitude");
        String longitude = SharedUtil.getString("longitude");
        searchUrl = Urls.SEARCH_FIND + "&latitude=" + latitude + "&limit=20&page=1&keyword=" +
                keyword + "&type=" + type + "&longitude=" + longitude;
        loadData();

    }

    private void loadData() {

        NetworkUtil.getInstance().getSearchResult(searchUrl, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                progressDialog.dismiss();
                Gson gson = new Gson();
                mList.clear();
                sAdapter.notifyDataSetChanged();
                switch (type) {
                    case 1:
                        FindHospital findHospital = gson.fromJson(responseInfo.result.toString(), FindHospital.class);
                        if (findHospital != null && findHospital.getStatus() == 10000) {
                            mList.addAll(findHospital.getData().getList());
                            MyListViewAdapter adapter = new MyListViewAdapter(SearchActivity.this, mList);
                            lv_search.setAdapter(adapter);
                        }
                        break;
                    case 2:
                        FindDoctor findDoctor = gson.fromJson(responseInfo.result.toString(), FindDoctor.class);
                        if (findDoctor != null && findDoctor.getStatus() == 10000) {
                            mList.addAll(findDoctor.getData().getList());
                            FindDoctorAdapter adapter = new FindDoctorAdapter(SearchActivity.this, mList);
                            lv_search.setAdapter(adapter);
                        }
                        break;
                    case 3:
                        Disease disease = gson.fromJson(responseInfo.result.toString(), Disease.class);
                        if (disease != null && disease.getStatus() == 10000) {
                            mList.addAll(disease.getData().getList());
                            DiseaseAdapter adapter = new DiseaseAdapter(SearchActivity.this, mList);
                            lv_search.setAdapter(adapter);
                        }
                        break;
                }
                sAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        type = 3;
        tv_type.setText("查疾病");
        et_search.setText(hotWords[position]);
        tv_hint.setText( hotWords[position] + "的搜索结果为");
        tv_hint.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        startKeySearch();
    }
}
